package 逆波兰计数器;

import StackDemo.StackEmptyException;

import java.util.Arrays;
import java.util.Scanner;

class Test{
    public static void main(String[] args) {
        String str = "7*2*2-5+1";
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operaStack = new ArrayStack(10);
        String s = "";
        int nums1 = 0;
        int nums2 = 0;
        int index = 0;
        int opera = 0;
        char ch = ' ';
        while (true) {
            ch = str.charAt(index);
            // 判断该字符是否是符号
            if(operaStack.isOpera(ch)){
                // 判断符号栈是否为空
                if(!operaStack.isEmpty()){
                    // 判断优先级
                    if(operaStack.priority(ch) <= operaStack.priority(operaStack.peek())){
                        nums1 = numStack.pop();
                        nums2 = numStack.pop();
                        opera = operaStack.pop();
                        int cal = numStack.cal(nums1, nums2, opera);
                        numStack.push(cal);
                        operaStack.push(ch);
                    }else{
                        operaStack.push(ch);
                    }
                }else{
                    // 符号栈为空,直接入栈
                    operaStack.push(ch);
                }
            }else{
                boolean flag = true;// 定义标志位 检查字符串是否达到末尾

                // 处理非个位数的情况 如 88 66 这样的数
                while (!operaStack.isOpera(ch)) {
                    s+=ch;
                    if(index == str.length() -1 ){
                        numStack.push(Integer.parseInt(s));
                        flag = false;
                        break;
                    }else {
                        index++;
                        ch = str.charAt(index);
                    }
                }
                if(!flag){
                    break;
                }
                numStack.push(Integer.parseInt(s));
                s = "";
                index--;
            }
            index++;
            if(index>=str.length()){
                break;
            }
        }

        while (true) {
            if(operaStack.isEmpty()){
                System.out.println(str+"="+numStack.pop());
                break;
            }
            nums1 = numStack.pop();
            nums2 = numStack.pop();
            opera = operaStack.pop();
            int cal = numStack.cal(nums1, nums2, opera);
            numStack.push(cal);
        }
    }

}



public class ArrayStack {
    private final int capacity;
    private int top = -1;
    private int[] stack ;

    public ArrayStack(int capacity){
        this.capacity = capacity;
        stack = new int[capacity];
    }

    // 入栈
    public void push(int data){
        if(isFull()){
            stack = Arrays.copyOf(stack,capacity*2);
        }
        top++;
        stack[top] = data;
    }

    // 出栈
    public int pop(){
        if(isEmpty()){
            throw new StackEmptyException("队列为空,无法删除元素");
        }

        int value = stack[top];
        top--;
        return value;
    }

    // 查看栈顶的值
    public int peek(){
        return stack[top];
    }

    // 制定优先级规则
    public int priority(int opera){
        if(opera == '*' || opera == '/'){
            return 1;
        }else if(opera == '+' || opera == '-'){
            return 0;
        }else{
            return -1;
        }
    }


    // 判断是否为运算符
    public boolean isOpera(int val){
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    // 运算方法
    public int cal(int num1,int num2,int opera){
        return  switch (opera) {
            case '+' -> num1 + num2;
            case '-' -> num2 - num1;
            case '*' -> num1 * num2;
            case '/' -> num2 / num1;
            default -> -1;
        };
    }

    public boolean isFull(){
        return top == capacity - 1;
    }
    public boolean isEmpty(){
        return top == - 1;
    }
}
